<?php

namespace App\Http\Controllers;

use App\Models\Company;
use App\Models\User;
use App\Services\CommonService;
use App\Services\CompanyService;
use App\Services\UserService;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;

/**
 * 公司控制器类
 */
class CompanyController extends Controller
{
    public $userService;
    public $companyService;

    public function __construct(CompanyService $companyService, UserService $userService)
    {
        parent::__construct();
        $this->companyService = $companyService;
        $this->userService    = $userService;
    }

    /**
     * 通过用户注册公司
     */
    public function store(Request $request)
    {
        if (!is_phone_number($request->input('phone'))) {
            return $this->error('手机号错误');
        }
        if (!$user = UserService::getUserByPhone($request->input('phone'))) {
            return $this->error('手机号不存在，请先用手机号注册用户');
        }
        $result = $this->companyService->storeCompanyByUser($request->all(), $user->id);
        if ($result['msg']) {
            return $this->error($result['msg']);
        }

        return $this->success('','注册成功！');
    }

    /**
     * 公司列表
     */
    public function index(Request $request)
    {
        $perPage = $request->input('per_page') ?: $this->limit;

        $list = $this->companyService->getList($request, $perPage);

        return $this->success($list);
    }

    /**
     * 获取一条公司信息
     */
    public function show(Request $request)
    {
        $id = $request->input('id');

        $info = $this->companyService->getInfo($id);

        return $this->success($info);
    }

    /**
     * 公司编辑
     */
    public function update(Request $request)
    {
        $id = $request->input('id');
        $result = $this->companyService->updateData($id, $request->all());
        if ($result['msg']) {
            return $this->error('修改失败');
        }

        return $this->success('', '修改成功！');
    }

    /**
     * 批量软删除，ids可以是以","逗号分割的字符串，也可以是数组形式
     *
     * @param Request $request
     * @return JsonResponse
     * @author: lxk
     * @date: 2024/2/26 11:03
     */
    public function destroyMore(Request $request) : JsonResponse
    {
        $ids = to_array($request->input('ids'));
        if (!Company::destroy($ids)) {
            return $this->error('删除失败！');
        };
        $ids = implode(',', $ids);
        CommonService::addActionLog("删除公司", "【数据ID={$ids}】");

        return $this->success('', '已放入回收站');
    }

    /**
     * 查询公司下的用户
     */
    public function users(Request $request)
    {
        $companyId = company_id();

        $users = $this->companyService->users($companyId);

        return $this->success($users);
    }

    /**
     * 查询公司下的部门 (当前登录用户所在的公司)
     */
    public function departments(Request $request)
    {
        $companyId = $request->input('company_id', 0);

        $list = $this->companyService->getDepartments($companyId);

        return $this->success($list);
    }

    /**
     * 公司人员授权
     */
    public function auth(Request $request)
    {
        $userId    = $request->input('userId');
        $companyId = $request->input('id');
        $roleIds   = to_array($request->input('roleIds'));
        $result    = $this->companyService->auth($companyId, $userId, $roleIds);
        if ($result['msg']) {
            return $this->error($result['msg']);
        }
        $userName = User::query()->where('id', $userId)->value('nickname');
        CommonService::addActionLog('公司人员授权', "用户名:{$userName}");

        return $this->success('', '授权成功!');
    }

    /**
     * 取得公司下的用户和用户角色
     */
    public function userRoles(Request $request)
    {
        $companyId = $request->input('id', 0);
        $userRoles = $this->companyService->userRoles($companyId);

        return $this->success($userRoles);
    }

    /**
     * 审核公司
     */
     public function review(Request $request)
     {
         $companyId = $request->input('id');
         $status    = $request->input('status');
         if (!$this->companyService->audit($companyId, $status)) {
             return $this->error('操作失败!');
         }

         return $this->success('', '操作成功！');
     }

}
